﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Tycho.Parser.Peg;
using Tycho.Parser.Tokens;
using Tycho.Lexer;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class IdentifierTerminalSpecification {
        [Test]
        public void ShouldReturnNewIdentifierTermAndIncrementIndexIfIdentifierToken () {
            var id = new IdentifierTerminal<object> ();

            List<Token> tokens = NativeLexer.Lex ("a b c");

            var result = id.Parse (tokens.ToArray (), 0, null);

            Assert.AreEqual (1, result.Index);
            Assert.IsTrue (result.Term is IdentifierTerm);
            Assert.AreEqual ("a", ((IdentifierTerm) result.Term).Name);
        }

        [Test]
        public void ShouldReturnNullAndNotIncrementIndexIfNotIdentifierToken () {
            var id = new IdentifierTerminal<object> ();

            List<Token> tokens = NativeLexer.Lex ("8 b c");

            var result = id.Parse (tokens.ToArray (), 0, null);
            Assert.IsNull (result);
        }

        [Test]
        public void ShouldReturnNullIfIndexIsOutOfTokenArray () {
            var id = new IdentifierTerminal<object> ();

            List<Token> tokens = NativeLexer.Lex ("a b c");

            var result = id.Parse (tokens.ToArray (), 3, null);
            Assert.IsNull (result);
        }
    }
}